/*全局常量配置文件 2021-4-1 cgq*/
const colorList = [
  "#39B3FF",
  "#47E0E0",
  "#7891D9",
  "#83D978",
  "#C7A530",
  "#FF8439",
];

class ecahrData {
  rechibang(data) {
    return {
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "0%",
        right: "0%",
        bottom: "7%",
        top: "7%",
      },
      xAxis: [
        {
          type: "category",
          data: data.xAxis,
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          show: false,
        },
      ],

      series: [
        {
          type: "bar",
          barWidth: "30px",
          label: {
            show: true,
            position: "top",
            formatter: "{b}",
            valueAnimation: true,
          },
          itemStyle: {
            normal: {
              color: (params) => {
                return colorList[params.dataIndex];
              },
            },
          },
          data: data.series,
        },
      ],
    };
  }

  zjlr(data) {
    const jhnkse = [];
    data.map((a) => {
      jhnkse.push((a / 10000).toFixed(0));
    });
    return {
      grid: {
        left: "0%",
        right: "0%",
        bottom: "8%",
        top: "8%",
      },
      xAxis: {
        type: "category",
        data: ["净超大", "净大单", "净中单", "净小单"],
      },
      yAxis: {
        type: "value",
      },
      series: [
        {
          barWidth: "40px",

          itemStyle: {
            normal: {
              color: (params) => {
                let jhse = "red";
                if (params.data < 0) {
                  jhse = "green";
                }
                return jhse;
              },
              label: {
                show: true,
                position: "top",
                formatter: " {c}万",
              },
            },
          },
          data: jhnkse,
          type: "bar",
        },
      ],
    };
  }
  GetUpDownData(data) {
    return {
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "0%",
        right: "0%",
        top: "8%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "涨停",
            "涨停~5%",
            "5%~1%",
            "1%~0%",
            "平盘",
            "0%~-1%",
            "-1%~5%",
            "-5%~跌停",
            "跌停",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "涨跌统计",
          type: "bar",
          barWidth: "25px",
          data: data,
          label: {
            show: true,
            position: "top",
          },
          itemStyle: {
            normal: {
              color: (params) => {
                let jhse = "#E94848";
                if (params.dataIndex == 4) {
                  jhse = "#999";
                }
                if (params.dataIndex > 4) {
                  jhse = "#4DBF41";
                }
                return jhse;
              },
            },
          },
        },
      ],
    };
  }

  setDateLine(Data, ty) {

    var data = splitData(Data)
    console.log(data);
    //数组处理
    function splitData(rawData) {
      var datas = [];
      var times = [];
      var vols = [];
      var macds = []; var difs = []; var deas = [];
      for (var i = 0; i < rawData.length; i++) {
        datas.push(rawData[i]);
        times.push(rawData[i].splice(0, 1)[0]);
        vols.push(rawData[i][4]);
        macds.push(rawData[i][6]);
        difs.push(rawData[i][7]);
        deas.push(rawData[i][8]);
      }
      return {
        datas: datas,
        times: times,
        vols: vols,
        macds: macds,
        difs: difs,
        deas: deas
      };
    }

    //分段计算
    function fenduans() {
      var markLineData = [];
      var idx = 0; var tag = 0; var vols = 0;
      for (var i = 0; i < data.times.length; i++) {
        //初始化数据
        if (data.datas[i][5] != 0 && tag == 0) {
          idx = i; vols = data.datas[i][4]; tag = 1;
        }
        if (tag == 1) { vols += data.datas[i][4]; }
        if (data.datas[i][5] != 0 && tag == 1) {
          markLineData.push([{
            xAxis: idx,
            yAxis: data.datas[idx][1] > data.datas[idx][0] ? (data.datas[idx][3]).toFixed(2) : (data.datas[idx][2]).toFixed(2),
            value: vols
          }, {
            xAxis: i,
            yAxis: data.datas[i][1] > data.datas[i][0] ? (data.datas[i][3]).toFixed(2) : (data.datas[i][2]).toFixed(2)
          }]);
          idx = i; vols = data.datas[i][4]; tag = 2;
        }

        //更替数据
        if (tag == 2) { vols += data.datas[i][4]; }
        if (data.datas[i][5] != 0 && tag == 2) {
          markLineData.push([{
            xAxis: idx,
            yAxis: data.datas[idx][1] > data.datas[idx][0] ? (data.datas[idx][3]).toFixed(2) : (data.datas[idx][2]).toFixed(2),
            value: (vols / (i - idx + 1)).toFixed(2) + ' M'
          }, {
            xAxis: i,
            yAxis: data.datas[i][1] > data.datas[i][0] ? (data.datas[i][3]).toFixed(2) : (data.datas[i][2]).toFixed(2)
          }]);
          idx = i; vols = data.datas[i][4];
        }
      }
      return markLineData;
    }

    //MA计算公式
    function calculateMA(dayCount) {
      var result = [];
      for (var i = 0, len = data.times.length; i < len; i++) {
        if (i < dayCount) {
          result.push('-');
          continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
          sum += data.datas[i - j][1];
        }
        result.push((sum / dayCount).toFixed(2));
      }
      return result;
    }
    // 1111
    var option = {
      title: {
        text: 'K线周期图表',
        left: 0
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'line'
        },
        formatter: function (params) {
          console.log(params);
          var res = params[0].name;
          for (var i = params.length - 1; i >= 0; i--) {
            if (params[i].value instanceof Array) {
              res = `
        ${res}<br>
       开盘 :${params[i].value[0]} <br> 
       现价 :${params[i].value[1]}<br>
       最低 :${params[i].value[2]}<br> 
       最高 :${params[i].value[3]}<br> 
       振幅 ：${params[i].value[6]}%<br> 
       涨幅 ：${params[i].value[7]}%<br> 
       涨幅额 ：${params[i].value[8]}<br> 
  `;
            }
          }
          //  try{
          //       th.ma5 = params[1].value.toFixed(2)
          //   th.ma10 = params[2].value.toFixed(2)
          //   th.ma20 = params[3].value.toFixed(2)
          //  }catch(e){}
          return res;
        },

      },
      legend: {
        data: ['KLine', 'MA5']
      },
      grid: [{
        left: '3%',
        right: '1%',
        height: '60%'
      }, {
        left: '3%',
        right: '1%',
        top: '71%',
        height: '10%'
      }, {
        left: '3%',
        right: '1%',
        top: '82%',
        height: '14%'
      }],
      xAxis: [{
        type: 'category',
        data: data.times,
        scale: true,
        boundaryGap: false,
        axisLine: { onZero: false },
        splitLine: { show: false },
        splitNumber: 20,
        min: 'dataMin',
        max: 'dataMax'
      }, {
        type: 'category',
        gridIndex: 1,
        data: data.times,
        axisLabel: { show: false }
      }, {
        type: 'category',
        gridIndex: 2,
        data: data.times,
        axisLabel: { show: false }
      }],
      yAxis: [{
        scale: true,
        splitArea: {
          show: false
        }
      }, {
        gridIndex: 1,
        splitNumber: 3,
        axisLine: { onZero: false },
        axisTick: { show: false },
        splitLine: { show: false },
        axisLabel: { show: true }
      }, {
        gridIndex: 2,
        splitNumber: 4,
        axisLine: { onZero: false },
        axisTick: { show: false },
        splitLine: { show: false },
        axisLabel: { show: true }
      }],
      dataZoom: [{
        type: 'inside',
        xAxisIndex: [0, 0],
        start: 20,
        end: 100
      }, {
        show: true,
        xAxisIndex: [0, 1],
        type: 'slider',
        top: '97%',
        start: 20,
        end: 100
      }, {
        show: false,
        xAxisIndex: [0, 2],
        type: 'slider',
        start: 20,
        end: 100
      }],
      series: [{
        name: 'K线周期图表',
        type: 'candlestick',
        data: data.datas,
        itemStyle: {
          normal: {
            color: '#ef232a',
            color0: '#14b143',
            borderColor: '#ef232a',
            borderColor0: '#14b143'
          }
        },
        markArea: {
          silent: true,
          itemStyle: {
            normal: {
              color: 'Honeydew'
            }
          },
          data: fenduans()
        },
        markPoint: {
          data: [
            { type: 'max', name: '最大值' },
            { type: 'min', name: '最小值' }
          ]
        },
        markLine: {
          silent: true,
          // 去掉箭头
          symbol: "none",
          data: [
            [
              {
                symbol: "none",
                x: "0%",
                yAxis: "max",
              },
              {
                symbol: "circle",
                type: "max",
              },
            ],
            [
              {
                symbol: "none",
                x: "0%",
                yAxis: "min",
              },
              {
                symbol: "circle",
                type: "min",
                name: "最高点",
              },
            ],
            [
                
              {
                type: "max",
               
              },
              {
                type: "min"
              },
            ],
          ],
          lineStyle: {
            normal: {
              type: "dashed",
              color: "red",
            },
          },
          label: {
            formatter: "",
          },
        },
      }, {
        name: 'MA5',
        type: 'line',
        data: calculateMA(5),
        smooth: true,
        lineStyle: {
          normal: {
            opacity: 0.5
          }
        }
      }, {
        name: 'Volumn',
        type: 'bar',
        xAxisIndex: 1,
        yAxisIndex: 1,
        data: data.vols,
        itemStyle: {
          normal: {
            color: function (params) {
              var colorList;
              if (data.datas[params.dataIndex][1] > data.datas[params.dataIndex][0]) {
                colorList = '#ef232a';
              } else {
                colorList = '#14b143';
              }
              return colorList;
            },
          }
        }
      }, {
        name: 'MACD',
        type: 'bar',
        xAxisIndex: 2,
        yAxisIndex: 2,
        data: data.macds,
        itemStyle: {
          normal: {
            color: function (params) {
              var colorList;
              if (params.data >= 0) {
                colorList = '#ef232a';
              } else {
                colorList = '#14b143';
              }
              return colorList;
            },
          }
        }
      }, {
        name: 'DIF',
        type: 'line',
        xAxisIndex: 2,
        yAxisIndex: 2,
        data: data.difs
      }, {
        name: 'DEA',
        type: 'line',
        xAxisIndex: 2,
        yAxisIndex: 2,
        data: data.deas
      }
      ]
    }

    return option
  }

  setDateLine22222(Data, ty) {
    const upColor = "#47B262";
    const downColor = "#EB5454";
    const seriesData = [];
    const xTime = [];
    const calculateMA = (dayCount) => {
      var result = [];
      for (var i = 0, len = Data.length; i < len; i++) {
        if (i < dayCount) {
          result.push("-");
          continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
          sum += +Data[i - j][2];
        }
        result.push(sum / dayCount);
      }
      return result;
    };

    const calculatebar = () => {
      let jjbse = [];
      Data.map((a) => {
        jjbse.push({
          value: a[5],
          itemStyle: {
            color: a[1] - a[2] < 0 ? downColor : upColor,
          },
        });
      });
      return jjbse;
    };

    // if(ty){
    //   let zh = Data.length-1
    //   let hbbse = Data[zh]
    //   seriesData[zh] = hbbse
    // }else{

    // }

    Data.map((a) => {
      xTime.push(a[0]);
      seriesData.push([a[1], a[2], a[4], a[3], a[5], a[7], a[8], a[9]]);
    });

    let okl = {
      min: "",
      max: "",
      xAxis: [
        {
          type: "category",
          gridIndex: 0,
          data: xTime,
          show: false,
        },
        {
          type: "category",
          gridIndex: 1,
          boundaryGap: false,
          axisLine: {
            onZero: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          min: "dataMin",
          max: "dataMax",
        },
      ],
      yAxis: [
        {
          scale: true,
          splitArea: {
            show: true,
          },
          axisLabel: {},
          gridIndex: 0,
        },
        {
          scale: true,
          gridIndex: 1,
          splitNumber: 2,
          axisLabel: {
            show: false,
          },
          axisLine: {
            show: false,
          },

          splitLine: {
            show: false,
          },
        },
      ],
      legend: {
        data: ["日K", "MA5", "MA10", "MA20", "MA30"],
      },
      grid: [
        {
          left: "1%",
          right: "2%",
          bottom: "15%",
          top: "6%",
          containLabel: true,
        },
        {
          left: "2%",
          right: "2%",
          top: "80%",
          bottom: 0,
          containLabel: true,
        },
      ],

      tooltip: {
        // 加上这个鼠标滑动到该柱子上提示
        trigger: "axis",
        formatter: function (params) {
          var res = params[0].name;
          for (var i = params.length - 1; i >= 0; i--) {
            if (params[i].value instanceof Array) {
              res = `
      ${res}${params[i].seriesName}<br>
     开盘 :${params[i].value[1]} <br> 
     现价 :${params[i].value[2]}<br>
     最低 :${params[i].value[3]}<br> 
     最高 :${params[i].value[4]}<br> 
     振幅 ：${params[i].value[6]}%<br> 
     涨幅 ：${params[i].value[7]}%<br> 
     涨幅额 ：${params[i].value[8]}<br> 
     成交量 :${(params[i].value[5] / 10000).toFixed(2)}万
`;
            }
          }
          //  try{
          //       th.ma5 = params[1].value.toFixed(2)
          //   th.ma10 = params[2].value.toFixed(2)
          //   th.ma20 = params[3].value.toFixed(2)
          //  }catch(e){}
          return res;
        },
      },
      //                    滑动的时候竖线关联竖线
      axisPointer: {
        link: [
          {
            xAxisIndex: "all",
          },
        ],
        label: {
          backgroundColor: "#777",
        },
      },
      //                    两个图表关联缩放
      dataZoom: [
        {
          type: "inside",
          xAxisIndex: [0, 1],
          startValue: 900,
          endValue: 1000,
        },
        {
          show: true,
          xAxisIndex: [0, 1],
          type: "slider",
          top: "100%",
          startValue: 900,
          endValue: 1000,
        },
      ],
      series: [
        {
          type: "candlestick",
          name: "日线",
          data: seriesData, // 二维数组  0等于开盘价  1等于收盘价  2等于最低价 3等于最高价

          markLine: {
            symbol: ["none", "none"],

            data: [
              [{
                symbol: "none",
                x: "100%",

                yAxis: "average",
              }, { type: 'average', name: '平均值', valueDim: "highest", }],

              [
                {
                  symbol: "none",
                  x: "82%",

                  yAxis: "max",
                },

                {
                  symbol: "circle",
                  valueDim: "highest",
                  label: {
                    position: "start",
                    formatter: (a) => {
                      okl.max = a.value
                      return ''
                    },
                  },
                  type: ['max'],
                  name: "最高点",
                },
              ],
              [
                {
                  //最高点和最低点连线
                  type: "min",
                  valueDim: "lowest",
                  symbol: "circle",
                  symbolSize: 10,
                  label: {
                    formatter: function (a, b) {
                      okl.min = a.value
                      return ''

                    },
                  },
                },

                {
                  type: "max",
                  valueDim: "highest",
                  symbol: "circle",
                },
              ],
            ],
          },

        },
        {
          name: "MA5",
          type: "line",
          showSymbol: false,
          data: calculateMA(5),
          color: "#000",
        },
        {
          name: "MA10",
          type: "line",
          showSymbol: false,
          data: calculateMA(10),
          color: "yellow",
        },
        {
          name: "MA20",
          type: "line",
          showSymbol: false,
          data: calculateMA(20),
          color: "#FF00FF",
        },

        {
          type: "bar",
          xAxisIndex: 1,
          yAxisIndex: 1,
          data: calculatebar(),
          markPoint: {
            data: [
              [
                {
                  type: "min",
                  valueDim: "highest",
                },
                {
                  type: "max",
                },
              ],
            ],
          },
          markLine: {
            lineStyle: {
              type: "dashed",
            },
            data: [
              [
                {
                  symbol: "none",
                  x: "92%",
                  yAxis: "max",
                },
                {
                  symbol: "circle",
                  label: {
                    position: "start",
                    formatter: function (a) {
                      return (a.value / 10000).toFixed(2) + "万";
                    },
                  },
                  type: "max",
                  name: "最高点",
                },
              ],
            ],
          },
        },
      ],
    };
    return okl
  }
}


const getkjhs = (okl) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(okl.min + ':' + okl.max)
    }, 3000);
  })
}
export default ecahrData;
